package CustomClassFile.CronJob;

import java.sql.CallableStatement;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


import net.sf.json.JSONObject;
import weaver.conn.ConnectionPool;
import weaver.conn.WeaverConnection;
import weaver.general.BaseBean;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.interfaces.schedule.CronJob;

public class Monthtotal extends BaseCronJob implements CronJob {

	public void execute() {
		long startTime = System.currentTimeMillis(); // 获取开始时间
		Map<String,Object> map=new HashMap<>();
		getAll();
		long endTime = System.currentTimeMillis();
		long usetime = (endTime - startTime) / 1000;
		map.put("usetime", usetime + "s");
		JSONObject jsonObject = JSONObject.fromObject(map) ;
		BaseBean writelog = new BaseBean();
		writelog.writeLog("三班倒出勤月结统计:"+jsonObject.toString());
	}

	private void getAll() {
		Date date=new Date();
		Date premonthdate=DateUtil.dateMonthCalculate(date, -1);//当前月份上一个月
		String yearMonth=DateUtil.dateString(premonthdate, "yyyy-MM");
		Date yearMonthpredate=DateUtil.dateMonthCalculate(premonthdate, -1);//当前月份上2个月
		String yearMonthpre=DateUtil.dateString(yearMonthpredate, "yyyy-MM");
		String sdate=yearMonth+"-01";
		String edate=yearMonth+"-"+getDayOfMonth(premonthdate);
		ConnectionPool connpool = ConnectionPool.getInstance();
		WeaverConnection conn = connpool.getConnection();
		 try {
				CallableStatement callstat=conn.prepareCall("{call zsws_sanbandaotongji(?,?,?,?)}");
				callstat.setString(1, yearMonth);
				callstat.setString(2, yearMonthpre);
				callstat.setString(3, sdate);
				callstat.setString(4, edate);
				callstat.execute();
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				conn.close();
			}
		
	}
	/**
	 *  java获取当前月的天数
	 * @param date yyyy-MM-dd
	 * @return
	 */
	private  int getDayOfMonth(Date date){
		Calendar aCalendar = Calendar.getInstance();
		aCalendar.setTime(date);
		int day=aCalendar.getActualMaximum(Calendar.DATE);
		return day;
	}
}
